* Create data for Matlab: STUPs
* 12 Jun 2020

	// NOTE: CHANGED A.H. 04/10/21

clear all
set more off

use "$input/PovertyTraps_structural.dta" , clear


* Restrict to stups
rename hhid5 hhid3

	/*
	cap drop _merge
	merge m:1 hhid3 using "$input/stup_hhid3.dta"
	keep if _merge==3
	drop _merge
	*/

keep if stup==1 & treat==1

* Keep relevant variables
keep hhid3 branchid survey_wave livestock_H wage_H pAssets* wageMA cows_no hiredInLabour
replace pAssets=pAssets*1000
replace pAssets0=pAssets0*1000
gen total_H = wage_H + livestock_H
order wage_H total_H, after(livestock_H)
order hhid3, first
replace livestock_H=0 if cows_no==0
drop cows_no

* Assign pre-transfer productive assets at baseline to all periods' pAssets0
bysort hhid3 (survey_wave) : replace pAssets0 = pAssets0[1]

	* Merge in max wage hours by branch

		/*		
		merge m:1 branchid using "$input/Max_wage_hours_by_branch.dta"
		keep if _merge==3
		drop _merge
		*/


* generate max hours by branch (90th percentile of hours worked among stups incl. zeros)
levelsof branchid , local(bs)
gen p90H =.
foreach b of local bs {
    su wage_H if survey==1 & branchid==`b' , d 
	replace p90H = r(p90) if branchid==`b'
}
rename p90H maxH 


* Generate cases
gen case = 1 if livestock_H>0 & wage_H>0 & hiredInLabour>0
replace case = 2 if livestock_H>0 & wage_H>0 & hiredInLabour==0
replace case = 3 if livestock_H>0 & wage_H==0 & hiredInLabour>0
replace case = 4 if livestock_H>0 & wage_H==0 & hiredInLabour==0
replace case = 5 if livestock_H==0 & wage_H>0 & hiredInLabour>0
replace case = 6 if livestock_H==0 & wage_H>0 & hiredInLabour==0
replace case = 7 if livestock_H==0 & wage_H==0

preserve
* Restrict to baseline and year 2 data
keep if survey == 1 | survey == 2

* Reshape to give one obs per HH
sort hhid3 survey_wave
reshape wide livestock_H wage_H total_H hiredInLabour pAssets wageMA case, i(hhid3) j(survey_wave)

* Drop if baseline or year 2 total hours exceed 3650 hours per year
drop if total_H1 > 3650
drop if total_H2 > 3650

save "$intermediate/data_for_matlab_stups_all.dta", replace

* Keep only cases where parameters are identified
keep if (case2 == 1) | (case2 == 2 & case1 >= 6) | (case2 == 3 & case1 >= 6)

* Extract data for structural analysis
drop branchid
export delimited using "$intermediate/data_for_matlab_stups.csv", novarnames replace
save "$intermediate/data_for_matlab_stups.dta", replace
restore

* Create data to calibrate parameters using w2&3, w3&4 and w4&5
forv i = 2/4 {
    local j = `i' + 1
	preserve
	* Restrict to wave `i' and wave `i' + 1
	keep if survey == `i' | survey == `j'

	* Reshape to give one obs per HH
	sort hhid3 survey_wave
	reshape wide livestock_H wage_H total_H hiredInLabour pAssets wageMA case, i(hhid3) j(survey_wave)

	* Drop if baseline or year 2 total hours exceed 3650 hours per year
	drop if total_H`i' > 3650
	drop if total_H`j' > 3650

	save "$intermediate/data_calib_w`j'_all.dta", replace

	* Keep only cases where parameters are identified
	keep if (case`j' == 1) | (case`j' == 2 & case`i' >= 6) | (case`j' == 3 & case`i' >= 6)

	* Extract data for structural analysis
	drop branchid
	export delimited using "$intermediate/data_calib_w`j'.csv", novarnames replace

	save "$intermediate/data_calib_w`j'.dta", replace
	restore
}
